package com.maretksa.absensi;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.SeleneseCommandExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebDriverCommandProcessor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import junit.framework.TestCase;
import com.google.common.base.Function;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;


public class Abstract extends TestCase {
	private static final String TIMEOUT = "timeout";
	private static final String WAIT_FOR_ELEMENT_VISIBLE = "Wait For Element Visible";
	protected static final String IKA_WIJANTI = "//div[contains(text(), 'Ika Wijayanti')]";
	protected static final String Alert = "//div[@class='pam login_error_box uiBoxRed']";
	protected static final String URL = "http://172.17.10.165/Attendance-UI.git/assets/www/html/Attendance.html#attendanceLoginFacebook";
	protected static final String ATTENDACE_MENU_BUTTON_LIST = "//div[@class='attendace-menu-button-list']";
	protected static final String SEARCH_BUTTON_TEXT = "//div[@class='search-button-text']";
	
//	DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*safari", "http://www.google.com");
//	CommandExecutor executor = new SeleneseCommandExecutor(selenium);
//	DesiredCapabilities dc = new DesiredCapabilities();
//	WebDriver browser = new RemoteWebDriver(executor, dc);
//	DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
	
	
	
	
//	DefaultSelenium selenium = new WebDriverBackedSelenium(browser, "http://www.google.com");
	
	/* login */ 
	String loginFB = "//div[@class='button-login-facebook']";
	String userNameFB = "//input[@class='inputtext']";
	String password = "//input[@class='inputpassword']";
	String masuk = "//input[@id='u_0_1']";
	String employeeID = "//input[@class='employee-id-input-bg']";
	String continueButton = "//div[@class='continue-button']";
	String checkIn = "//div[@class='attendance-menu-button-checkin']";
	String signOut = "//div[@class='attendance-footer-right']";
	protected static final String Hidden_Pass = "Allahu_Akbar";
	protected static final String HIdden_email = "nazgul_sick@yahoo.com";
	protected static final String INPUT_NAME_PASS = "//input[@name='pass']";
	protected static final String INPUT_NAME_EMAIL = "//input[@name='email']";

	/* Attandance List */
	String inputDatePicker = "//input[@class='attendance-list-date-start hasDatepicker']";
	String iconNextMount = "//span[@class='ui-icon ui-icon-circle-triangle-w']";
	String january = "//span[contains(text(), 'January')]";
	String date31 = "//a[contains(text(), '31')]";
	String untilDatePicker = "//input[@class='attendance-list-date-until hasDatepicker']";
	String date2 = "//a[contains(text(), '2')]";
	
	/* check Out Pulang*/
	String alreadyCheckIn = "//div[@class='avs-error-image']";
	String okCheckInButton = "//div[@style='']/div/div/div[2]";
	String checkOut = "//div[@class='attendance-menu-button-checkout']";
	
	/* check Profile */
	String profile = "//div[@class='attendance-menu-button-profile']";
	String attandanceList = "//div[@class='attendance-profile-button-attendance']";
	String dailyAttandance = "//div[@class='attendace-menu-button-attendance']";
	String husein = "//div[contains(text(),'Husin Basari')]";
	String Febrianto = "//div[contains(text(),'Febrianto')]";
	String backButton = "//div[@class='attendance-footer-left']";
	String searchEmployeeID = "//div[@class='content-dd']/div[4]";
	
	public int waitForText (int a) {
		for (int second = 0;; second++) {
			if (second >= 60) fail(TIMEOUT);
			try {if (second >= a -1) break;} catch(Exception e){}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return a;
	}
	public void waitForElement(DefaultSelenium selenium,String element) throws Exception {
		while (!selenium.isTextPresent(element)){
			waitForText(5);
			System.out.println(WAIT_FOR_ELEMENT_VISIBLE);
			try {if (selenium.isVisible(element))
			System.out.println("This element Visible : "+element);
			break;} catch (Exception e){}
			try {Thread.sleep(1000);}
			catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void waitSecond(WebDriver browser, String element) {
		WebElement checkThisElement = browser.findElement(By.xpath(element)); 
		while (!checkThisElement.isDisplayed()){
			waitForText(5);
			System.out.println(WAIT_FOR_ELEMENT_VISIBLE);
//			try {if (checkThisElement.isDisplayed())
//			System.out.println("This element has display: "+element);
//			break;
//			}catch (Exception e){}
		}
	}
	/* To wait Element Until Visible */
	public static WebElement explicitWait(WebDriver browser, final By by){
		Wait<WebDriver> wait = new FluentWait<WebDriver>(browser)
				.withTimeout(30, TimeUnit.SECONDS)
				.pollingEvery(5, TimeUnit.SECONDS)
				.ignoring(NoSuchElementException.class);
		WebElement element = wait.until(new Function<WebDriver, WebElement>() {
			@Override
			public WebElement apply(WebDriver browser) {
                return browser.findElement(by);
        }
		});
		return element;}
}

